package plugin;

import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class TylerPlugin implements IBarcodeFinder
{

	@Override
	public String getDescription(String barcode)
	{
		String result = "";
		try
		{
			int timeout = 2*1000;
			URL url = new URL("http://www.upcfoodsearch.com/upc-" + barcode.trim() + "/");
			Document doc = Jsoup.parse(url, timeout);
			
			Elements titles = doc.getElementsByTag("title");
			String title = titles.get(0).text();
						
			result = parseTitle(title);
		}
		catch (Exception e) { }
		
		return result;
	}

	private String parseTitle(String title)
	{
		String result = "";
		String [] tokens = title.split("UPC");
		
		try
		{
			result = tokens[0].trim();
		}
		catch (Exception e) {	}
		
		if (result.compareTo("Not Found") == 0)
			result = "";
		
		return result;
	}
	
	
	
}
